/**
 * 小说管理管理初始化
 */
var NovelMgr = {
    id: "NovelMgrTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    condition:{
        beginTime:"",
        endTime:"",
        snovelName:"",
        snovelAuthor:"",
        schapter:"",
        sstatus:"",
        splacedTop:"",
        srecommend:"",
        ssourcesId:""
    }
};

/**
 * 初始化表格的列
 */
NovelMgr.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '所属类别', field: 'type_id', align: 'center', valign: 'middle',sortable: true},
        {title: '书名', field: 'title', align: 'center', valign: 'middle',
            formatter: function(v, item) {
                if (v != null && v != '' && v.length > 10) {
                    return "<a title='" + v + "'>" + v.substring(0, 9) + '...' + "</a>";
                } else {
                    return v;
                }
            }
        },
        {title: '章节排序字段',field: 'chapter_sort',visible: false, align:'center', valign: 'middle'},
        {title: '章节', field: 'chapter', align: 'center', valign: 'middle',
            formatter: function(v, item) {
                if (v != null && v != '' && v.length > 15) {
                    return "<a title='" + v + "'>" + v.substring(0, 14) + '...' + "</a>";
                } else {
                    return v;
                }
            }
        },
        {title: '内容', field: 'content', align: 'center', valign: 'middle',
            formatter: function(v, item) {
                if (v != null && v != '' && v.length > 15) {
                    return "<a title='" + v + "'>" + v.substring(0, 14) + '...' + "</a>";
                } else {
                    return v;
                }
            }
        },
        {title: '小说作者', field: 'novelist', align: 'center', valign: 'middle'},
        {title: '排序', field: 'sort', align: 'center', valign: 'middle'},
        {title: '链接地址', field: 'link', align: 'center', valign: 'middle',
            formatter: function (v, item) {
                if (v != null && v != '') {
                    return "<a href='" + v + "' target=\"_blank\">" + v + "</a>";
                } else {
                    return v;
                }
            }
        },
        {title: '作者', field: 'author', align: 'center', valign: 'middle'},
        {title: '来源id', field: 'sources_id', align: 'center', valign: 'middle',sortable: true},
        {title: '点赞', field: 'thumb_up', align: 'center', valign: 'middle',sortable: true},
        {title: '是否置顶', field: 'placed_top', align: 'center', valign: 'middle'},
        {title: '是否推荐', field: 'recommend', align: 'center', valign: 'middle',sortable: true},
        {title: '状态', field: 'status', align: 'center', valign: 'middle',sortable: true},
        {title: '添加时间', field: 'create_date', align: 'center', valign: 'middle',sortable: true},
        {title: '修改时间', field: 'modify_date', align: 'center', valign: 'middle',sortable: true},

    ];
};

/**
 * 检查是否选中
 */
NovelMgr.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        NovelMgr.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加小说管理
 */
NovelMgr.openAddNovelMgr = function () {
    var index = layer.open({
        type: 2,
        title: '添加小说管理',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/novelMgr/novelMgr_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看小说管理详情
 */
NovelMgr.openNovelMgrDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '小说管理详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/novelMgr/novelMgr_update?id=' + NovelMgr.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除小说管理
 */
NovelMgr.delete = function () {
    if (this.check()) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/novelMgr/delete", function (data) {
                Feng.success("删除成功!");
                NovelMgr.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("novelMgrId",NovelMgr.seItem.id);
            ajax.start();
        }
        Feng.confirm("是否删除小说：" + NovelMgr.seItem.chapter + "?", operation)
    }
};
/**
 * @Author: Lzx
 * @Description: 查看小说详情
 * @Params:  * @param null
 * @Date: 22:10 2017/8/14
 */
NovelMgr.detail = function () {
    if (this.check()){
        var ajax = new $ax(Feng.ctxPath + "/novelMgr/detail/" + this.seItem.id, function(data) {
            Feng.infoDetail("小说详情",data.regularMessage);
        }, function (data){
            Feng.error("获取详情失败！");
        });
        ajax.start();
    }
}

/**
 * @Author: Lzx
 * @Description: 查询表单提交参数对象
 * @Params:  * @param null
 * @Date: 22:17 2017/8/14
 */
NovelMgr.formParams = function () {
    var queryData = {};
    queryData['snovelName'] = NovelMgr.condition.snovelName;
    queryData['beginTime'] = NovelMgr.condition.beginTime;
    queryData['endTime'] = NovelMgr.condition.endTime;
    queryData['snovelAuthor'] = NovelMgr.condition.snovelAuthor;
    queryData['schapter'] = NovelMgr.condition.schapter;
    queryData['sstatus'] = NovelMgr.condition.sstatus;
    queryData['splacedTop'] = NovelMgr.condition.splacedTop;
    queryData['srecommend'] = NovelMgr.condition.srecommend;
    queryData['ssourcesId'] = NovelMgr.condition.ssourcesId;

    return queryData;
}
/**
 * 查询小说管理列表
 */
NovelMgr.search = function () {
  //  var queryData = {};
  //  queryData['condition'] = $("#condition").val();
    NovelMgr.table.refresh({query: NovelMgr.formParams()});
};

$(function () {
    NovelMgr.app = new Vue({
        el: '#novelMgrPage',
        data: NovelMgr.condition
    })

    var defaultColunms = NovelMgr.initColumn();
    var table = new BSTable(NovelMgr.id, "/novelMgr/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(NovelMgr.formParams());
    NovelMgr.table = table.init();
});
